package mysql

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"user_srv/appconfig"
)

var (
	Db  *gorm.DB
	err error
)

func Mysql() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	mysqlConf := appconfig.AppConf.MysqlConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlConf.User, mysqlConf.Password, mysqlConf.Host, mysqlConf.Port, mysqlConf.Database)
	Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		logx.Info("MySQL连接信息失败", err)
		return
	}
	logx.Info("MySQL连接信息成功")
	err = Db.AutoMigrate(&User{}, &RealName{}, &House{}, &Order{})
	if err != nil {
		logx.Info("MySQL迁移信息失败", err)
		return
	}
	logx.Info("MySQL迁移信息成功")
}
